import {defineStore} from "pinia";
import axios from "axios";
import {reactive} from "vue";

/**
 *
 * 组合式的
 *
 */
// export const useTalkStore = defineStore('talk', {
//     actions: {
//         async addInfo() {
//             try {
//                 // 包裹式的解构赋值
//                 const {data: {content: title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json');
//                 talkStore.talkList.push({id : '002' ,content: title});
//             } catch (error) {
//                 alert(error.message);
//             }
//         }
//     },
//     state() {
//         return {
//             talkList: JSON.parse(localStorage.getItem('talk') as string) | []
//         }
//     }
// })

/**
 *
 * 组合式的
 *
 */
export const useTalkStore = defineStore('talk', () => {
    // const talkList = reactive(
    //     JSON.parse(localStorage.getItem('talk') as string) | []
    // );
    const talkList = reactive([
        {id : '001', content: '你笑起来真可爱'}
    ])
    async function addInfo() {
        try {
            // 包裹式的解构赋值
            const {data: {content: title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json');
            talkList.push({id : '002' ,content: title});
        } catch (error) {
            alert(error.message);
        }
    }
    // 没有语法糖的setup写法
    return {talkList, addInfo};
})